/**
 * 
 */
package com.wodsy.phoenix.datadao.cms.dao.impl;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.cms.dao.CmsHomePageShopRecDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.cms.constants.CmsContentStatus;
import com.wodsy.phoenix.dataobject.cms.model.IdxShopRecommended;

/**
 * @author bryan
 *
 */
@Repository
public class CmsHomePageShopRecDaoImpl extends BaseHibernateDao<IdxShopRecommended, Integer>
		implements CmsHomePageShopRecDao {

	@Autowired
    @Qualifier(value="cms")
	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Class<IdxShopRecommended> getModelClass() {
		return IdxShopRecommended.class;
	}
	
	@Override
	public IdxShopRecommended getOnlineShopReccommendedRec() {
		return (IdxShopRecommended) getSession().createCriteria(getModelClass())
				.add(Restrictions.eq("status", CmsContentStatus.ISONLINE.getKey()))
				.setMaxResults(1)
				.uniqueResult();
	}

	@Override
	public Pager<IdxShopRecommended> searchShopRecommendedRecByOrder(
			Pager<IdxShopRecommended> shopReccommendRes,
			IdxShopRecommended shopRecommendreq) {
		Criteria criteria = getSession().createCriteria(getModelClass());
		if(!StringUtils.isEmpty(shopRecommendreq.getShopName())){
			criteria.add(Restrictions.like("shopName", "%"+shopRecommendreq.getShopName()+"%"));
	    }
		if(!StringUtils.isEmpty(shopRecommendreq.getStatus())) {
			criteria.add(Restrictions.eq("status", shopRecommendreq.getStatus()));
		}
		if(!StringUtils.isEmpty(shopRecommendreq.getOnlineTime())&&!StringUtils.isEmpty(shopRecommendreq.getOfflineTime())) {
			criteria.add(Restrictions.le("onlineTime", shopRecommendreq.getOnlineTime()));
			criteria.add(Restrictions.ge("offlineTime", shopRecommendreq.getOfflineTime()));
		}else if(!StringUtils.isEmpty(shopRecommendreq.getOnlineTime())){
			criteria.add(Restrictions.le("onlineTime", shopRecommendreq.getOnlineTime()));
			criteria.add(Restrictions.ge("offlineTime", shopRecommendreq.getOnlineTime()));
		}
        criteria.addOrder(Property.forName("status").asc());
        return toPager(shopReccommendRes, criteria);
	}

	@Override
	public Integer updateGoodsRecommendedStatus(int shop_recId, int status) {
		Session session = getSession();
		String hqlUpdate =
				"update IdxShopRecommended t set t.status = :status,t.updateTime= :updateTime where t.id = :shop_recId";
		int updatedEntities = session.createQuery( hqlUpdate )
			.setInteger("status", status)
			.setTimestamp("updateTime", new Date())
			.setInteger("shop_recId", shop_recId)
			.executeUpdate();
		return updatedEntities;
	}
	
	@Override
	public Integer updateGoodsRecommendedStatus(Integer[] shop_recIds, int status) {
		Session session = getSession();
		String hqlUpdate =
				"update IdxShopRecommended t set t.status = :status,t.updateTime= :updateTime where t.id in (:shop_recIds)";
		int updatedEntities = session.createQuery( hqlUpdate )
			.setInteger("status", status)
			.setTimestamp("updateTime", new Date())
			.setParameterList("shop_recIds", shop_recIds)
			.executeUpdate();
		return updatedEntities;
	}

	@Override
	public Integer addNewShopRecommended(IdxShopRecommended shop) {
		Session session = getSession();
		session.save(shop);
		return shop.getId();
	}

	@Override
	public void updateNewShopRecommended(IdxShopRecommended shop) {
		Session session = getSession();
		session.update(shop);
	}
}
